package com.aloda.ojquestion.controller;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.ojcommon.anno.AuthCheck;
import com.aloda.ojcommon.constant.ErrorCode;
import com.aloda.ojcommon.constant.ExceptionConstant;
import com.aloda.ojcommon.domain.dto.PageQuery;
import com.aloda.ojcommon.domain.enums.UserRoleEnum;
import com.aloda.ojcommon.domain.result.PageResult;
import com.aloda.ojcommon.domain.result.Result;
import com.aloda.ojquestion.pojo.dto.QuestionAddDTO;
import com.aloda.ojquestion.pojo.dto.QuestionEditDTO;
import com.aloda.ojquestion.pojo.dto.QuestionPageDTO;
import com.aloda.ojquestion.pojo.dto.QuestionTableDTO;
import com.aloda.ojcommon.domain.entity.Question;
import com.aloda.ojquestion.pojo.vo.*;
import com.aloda.ojquestion.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author aloda
 * @Date 2025/4/26 8:54
 */
@Slf4j
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Resource
    private QuestionService questionService;

    /**
     * 分页查询题目
     * @param questionPageDTO
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @PostMapping("/page")
    public Result<PageResult<QuestionPageVO>> pageQuery(@RequestBody QuestionPageDTO questionPageDTO) {
        PageResult<QuestionPageVO> pageResult = questionService.pageQuery(questionPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 通过id查询题目信息
     * @param id
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @GetMapping("/{id}")
    public Result<QuestionInfoVO> getById(@PathVariable Long id) {
        Question question = questionService.getById(id);
        if (BeanUtil.isEmpty(question)) {
            return Result.error(ErrorCode.NOT_FOUND_ERROR.getCode(), ExceptionConstant.QUESTION_IS_NOT_EXIST);
        }
        return Result.success(QuestionInfoVO.of(question));
    }

    /**
     * 通过id获取题目和创建用户信息
     * @param id
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @GetMapping("/user/{id}")
    public Result<QuestionUserInfoVO> getQuestionUserInfoById(@PathVariable Long id) {
        return Result.success(questionService.getQuestionInfoById(id));
    }

    /**
     * 新增题目
     * @param questionAddDTO
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @PostMapping
    public Result<Long> addQuestion(@RequestBody QuestionAddDTO questionAddDTO) {
        log.info("新增题目：{}", questionAddDTO);
        return Result.success(questionService.addQuestion(questionAddDTO));
    }

    /**
     * 编辑题目
     * @param questionEditDTO
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @PutMapping
    public Result editQuestion(@RequestBody QuestionEditDTO questionEditDTO) {
        log.info("编辑题目：{}", questionEditDTO);
        questionService.editQuestion(questionEditDTO);
        return Result.success();
    }

    /**
     * 删除题目
     * @param ids
     * @return
     */
    @AuthCheck(role = UserRoleEnum.ADMIN)
    @DeleteMapping
    public Result deleteQuestion(@RequestBody List<Long> ids) {
        log.info("删除题目：{}", ids);
        questionService.deleteQuestion(ids);
        return Result.success();
    }

    /**
     * 分页查询主页的题目表格浏览信息
     * @param questionTableDTO
     * @return
     */
    @PostMapping("/page_table")
    public Result<PageResult<QuestionTableVO>> pageQueryQuestionTableVO(@RequestBody QuestionTableDTO questionTableDTO) {
        if (BeanUtil.isEmpty(questionTableDTO)
                || questionTableDTO.getPageSize() > PageQuery.MAX_PAGE_SIZE
                || questionTableDTO.getCurrentPage() > PageQuery.MAX_PAGE_NUM) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        PageResult<QuestionTableVO> pageResult = questionService.pageQueryQuestionTableVO(questionTableDTO);
        return Result.success(pageResult);
    }

    /**
     * 做题页面通过id查询题目详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Result<QuestionDetailVO> getQuestionDetailById(@PathVariable Long id) {
        if (id == null) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        QuestionDetailVO questionDetailVO = questionService.getQuestionDetailById(id);
        return Result.success(questionDetailVO);
    }
}
